package com.stu.thread.demo.producer_consumer_model;

import com.stu.thread.util.IdWorker;

/**
 * @projectName: learn_used_code
 * @packageName: com.stu.thread.demo.producer_consumer_model
 * @className: com.stu.thread.demo.producer_consumer_model-> TestThreadManageService
 * @createDate: 2021-12-20 18:11
 * @author: ZhangSheng
 * @version: 1.0
 * @description:  生产消费线程测试
 */
public class TestThreadManageService {


    public static void main(String[] args) throws InterruptedException {
        ThreadManagerService2 threadManagerService2 = new ThreadManagerService2();
        IdWorker idWorker = new IdWorker();
        new Thread(()->{
            for (int i = 0; i < 100; i++) {
                try {
                    String message = String.valueOf(idWorker.nextId());
                    threadManagerService2.put(message);
                    System.out.println(Thread.currentThread().getName() + ":" + message);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"producer1").start();


        new Thread(()->{
            for (;;){
                String message = null;
                try {
                    message = threadManagerService2.poll();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + ":" + message);
            }
        },"consumer1").start();

        new Thread(()->{
            for (;;){
                String message = null;
                try {
                    message = threadManagerService2.poll();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + ":" + message);
            }
        },"consumer2").start();

    }
}
